﻿/*!
 * jQuery cxDialog 1.2.1
 * http://code.ciaoca.com/
 * https://github.com/ciaoca/cxDialog
 * E-mail: ciaoca@gmail.com
 * Released under the MIT license
 * Date: 2015-01-13
 *
 * 绠€鏄撹皟鐢細$.cxDialog(string, [ok, no])
 * 瀹屾暣鏂规硶锛�$.cxDialog(opt)
 * @param {object|string} opt 鍙傛暟璁剧疆 or 鍐呭
 *   title {string} 鏍囬
 *   info {string|dom} 鍐呭
 *   ok {fn} 鐐瑰嚮纭鏃剁殑鍥炶皟鍑芥暟
 *   okText {string} 纭鎸夐挳鏂囧瓧
 *   no {fn} 鐐瑰嚮鍙栨秷鏃剁殑鍥炶皟鍑芥暟
 *   noText {string} 鍙栨秷鎸夐挳鏂囧瓧
 *   buttons {array} 鑷畾涔夋寜閽細[{text: 'text', callback: fn}, ...]
 *   closeBtn {boolean} 鏄惁鏄剧ず鍏抽棴鎸夐挳
 *   lockScroll {boolean} 鏄惁閿佸畾婊氬姩
 *   baseClass {string} 缁欏璇濇瀹瑰櫒澧炲姞 class锛屼笉浼氳鐩栭粯璁ょ殑 class
 *   background {string} 閬僵鑳屾櫙鐨勯鑹诧紝鐣欑┖琛ㄧず涓嶉渶瑕侀伄缃�
 *   width {int} 鎻愮ず妗嗗浐瀹氬搴�
 *   height {int} 鎻愮ず妗嗗浐瀹氶珮搴�
 *   zIndex {int} 鎻愮ず妗嗙殑灞傜骇
 *
 * @param {function} ok 鐐瑰嚮纭鏃剁殑鍥炶皟鍑芥暟
 * @param {function} no 鐐瑰嚮鍙栨秷鏃剁殑鍥炶皟鍑芥暟
 */
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else {
        factory(window.jQuery || window.Zepto || window.$);
    };
}(function ($) {
    if (typeof Zepto === 'function' || typeof Zepto === 'object') {
        // Add inner and outer width to zepto (adapted from https://gist.github.com/alanhogan/3935463)
        var ioDim = function (dimension, includeBorder) {
            return function (includeMargin) {
                var sides, size, elem;
                if (this) {
                    elem = this;
                    size = elem[dimension]();
                    sides = {
                        width: ['left', 'right'],
                        height: ['top', 'bottom']
                    };
                    sides[dimension].forEach(function (side) {
                        size += parseInt(elem.css('padding-' + side), 10);
                        if (includeBorder) {
                            size += parseInt(elem.css('border-' + side + '-width'), 10);
                        };
                        if (includeMargin) {
                            size += parseInt(elem.css('margin-' + side), 10);
                        };
                    });
                    return size;
                } else {
                    return null;
                };
            };
        };
        ['width', 'height'].forEach(function (dimension) {
            var Dimension = dimension.substr(0, 1).toUpperCase() + dimension.substr(1);
            Zepto.fn['inner' + Dimension] = ioDim(dimension, false);
            Zepto.fn['outer' + Dimension] = ioDim(dimension, true);
        });
    };

    var isIE6 = !!window.ActiveXObject && !window.XMLHttpRequest;

    var dialog = {
        dom: {},
        isElement: function (o) {
            if (o && (typeof HTMLElement === 'function' || typeof HTMLElement === 'object') && o instanceof HTMLElement) {
                return true;
            } else {
                return (o && o.nodeType && o.nodeType === 1) ? true : false;
            };
        },
        isJquery: function (o) {
            return (o && o.length && (typeof jQuery === 'function' || typeof jQuery === 'object') && o instanceof jQuery) ? true : false;
        },
        isZepto: function (o) {
            return (o && o.length && (typeof Zepto === 'function' || typeof Zepto === 'object') && Zepto.zepto.isZ(o)) ? true : false;
        }
    };

    dialog.init = function (opt, ok, no) {
        var _this = this;

        _this.dom.docHtml = $('html');
        _this.dom.box = $('<div></div>', { 'id': 'cxdialog', 'class': 'cxdialog' });
        _this.dom.overlay = $('<div></div>', { 'id': 'cxdialog_overlay', 'class': 'cxdialog_overlay' });
        _this.dom.holder = $('<div></div>', { 'id': 'cxdialog_holder', 'class': 'cxdialog_holder' });
        _this.dom.title = $('<div></div>', { 'class': 'cxdialog_title' });
        _this.dom.info = $('<div></div>', { 'class': 'cxdialog_info' });
        _this.dom.btns = $('<div></div>', { 'class': 'cxdialog_btns' });
        _this.dom.closeBtn = $('<a></a>', { 'rel': 'cxdialog', 'rev': 'close' });

        _this.dom.box.on('click', 'a', function () {
            var _rel = this.rel;
            var _rev = this.rev;
            var _result;

            if (_rel !== 'cxdialog') { return };

            if (_rev === 'close') {
                $("#cxdialog").attr("style", "").removeClass("forWin");
                $.cxDialog.defaults.baseClass = "";
                $.cxDialog.defaults.width = 0;
                $.cxDialog.defaults.height = 0;
                _this.exit();

            } else {
                if (_rev === 'btn_ok' || _rev === 'btn_no') {
                    //$("#cxdialog").attr("style", "");
                    $.cxDialog.defaults.baseClass = "";
                    $.cxDialog.defaults.width = 0;
                    $.cxDialog.defaults.height = 0;
                }
                for (var i = 0, l = _this.btnCache.length; i < l; i++) {
                    if (_this.btnCache[i].name === _rev && typeof _this.btnCache[i].callback === 'function') {
                        _result = _this.btnCache[i].callback();
                        break;
                    };
                };
            };

            if (_result !== false) {
                _this.exit();
            };

            return false;
        });
    };

    dialog.format = function (opt, ok, no) {
        var _this = this;
        _this.exit();

        if (typeof opt === 'string' && !opt.length) { return };

        if (typeof opt === 'string' || _this.isElement(opt) || _this.isJquery(opt) || _this.isZepto(opt)) {
            opt = {
                info: opt
            };
        } else if (typeof opt !== 'object') {
            return;
        };

        if (typeof ok === 'function') { opt.ok = ok };
        if (typeof no === 'function') { opt.no = no };

        opt = $.extend({}, $.cxDialog.defaults, opt);

        _this.setContent(opt);
        _this.show(opt);
    };

    // 璁剧疆鍐呭
    dialog.setContent = function (opt) {
        var _this = this;
        var _timeStamp = new Date().getTime();

        // 璁剧疆鏍囬
        if (typeof opt.title === 'string' && opt.title.length) {
            _this.dom.title.html(opt.title).appendTo(_this.dom.box);
        };

        // 璁剧疆鏂囨湰鍐呭
        _this.infoCache = undefined;

        if (typeof opt.info === 'string' && opt.info.length) {
            _this.dom.info.html(opt.info).appendTo(_this.dom.box);

            // 璁剧疆鍐呭涓� DOM 鍏冪礌鎴� jQuery 瀵硅薄
        } else if (_this.isElement(opt.info) || _this.isJquery(opt.info) || _this.isZepto(opt.info)) {
            if (_this.isElement(opt.info)) {
                _this.infoCache = $(opt.info);
            } else {
                _this.infoCache = opt.info;
            };

            var _cssFloat = _this.infoCache.css('float');
            var _cssDisplay = _this.infoCache.css('display');
            var _cssVisibility = _this.infoCache.css('visibility');

            _this.dom.holder.css({
                'float': _cssFloat,
                'display': _cssDisplay,
                'visibility': _cssVisibility,
                'width': _this.infoCache.outerWidth(),
                'height': _this.infoCache.outerHeight()
            }).data({
                'float': _cssFloat,
                'display': _cssDisplay,
                'visibility': _cssVisibility
            }).insertAfter(_this.infoCache);

            _this.infoCache.show().appendTo(_this.dom.box);

        } else {
            opt.info = String(opt.info);
            _this.dom.info.html(opt.info).appendTo(_this.dom.box);
        };

        // 璁剧疆鎸夐挳
        _this.btnCache = [];

        if (typeof opt.no === 'function') {
            _this.btnCache.push({
                name: 'btn_no',
                className: 'btn_no',
                text: opt.noText,
                callback: opt.no
            });
        };

        if (typeof opt.ok === 'function') {
            _this.btnCache.push({
                name: 'btn_ok',
                className: 'btn_ok',
                text: opt.okText,
                callback: opt.ok
            });
        };

        for (var i = 0, l = opt.buttons.length; i < l; i++) {
            _this.btnCache.push({
                name: 'btn_' + _timeStamp + '_' + i,
                className: 'btn_' + i,
                text: opt.buttons[i].text,
                callback: opt.buttons[i].callback
            });
        };

        if (_this.btnCache.length) {
            var _html = '';
            for (var i = 0, l = _this.btnCache.length; i < l; i++) {
                _html += '<a class="' + _this.btnCache[i].className + '" rel="cxdialog" rev="' + _this.btnCache[i].name + '">' + _this.btnCache[i].text + '</a>';
            };
            _this.dom.btns.html(_html).appendTo(_this.dom.box);
        };

        // 鍏抽棴鎸夐挳
        if (opt.closeBtn) {
            _this.dom.closeBtn.appendTo(_this.dom.box);
        };
    };

    // 鏄剧ず瀵硅瘽妗�
    dialog.show = function (opt) {
        var _this = this;
        _this.dom.box.appendTo('body');

        if (opt.lockScroll === true) {
            _this.dom.docHtml.addClass('cxdialog_lock');
        };

        if (typeof opt.background && opt.background.length) {
            _this.dom.overlay.css('background', opt.background).appendTo('body').css('display', 'block');
        };

        _this.dom.box.css('display', 'block');

        if (typeof opt.baseClass === 'string' && opt.baseClass.length) {
            _this.dom.box.addClass(opt.baseClass);
        };

        var _cssAttr = {};

        // IE6 涓嶆敮鎸� fixed锛岃缃綋鍓嶄綅缃�
        if (isIE6) {
            _cssAttr.top = document.documentElement.scrollTop + window.screen.availHeight / 4;
        };

        if (opt.width > 0) {
            _cssAttr.width = opt.width;
            _cssAttr.marginLeft = -(opt.width / 2);
        } else {
            _cssAttr.marginLeft = -(_this.dom.box.outerWidth() / 2);
        };

        if (opt.height > 0) {
            _cssAttr.height = opt.height;
            _cssAttr.marginTop = -(opt.height / 2);
        } else {
            _cssAttr.marginTop = -(_this.dom.box.outerHeight() / 2);
        };

        if (opt.zIndex > 0) {
            _cssAttr.zIndex = opt.zIndex;
        };

        _this.dom.box.css(_cssAttr);

    };

    // 褰掕繕鍔犺浇鐨� DOM
    dialog.backDom = function () {
        var _this = this;

        if (_this.isJquery(_this.infoCache) || _this.isZepto(_this.infoCache)) {
            _this.infoCache.css({
                'float': '',
                'display': '',
                'visibility': ''
            }).insertAfter(_this.dom.holder);

            _this.dom.holder.remove();
        };
    };

    // 鍏抽棴瀵硅瘽妗�
    dialog.exit = function () {
        var _this = this;

        _this.backDom();

        _this.infoCache = undefined;
        _this.btnCache = undefined;

        _this.dom.box.attr('class', 'cxdialog').empty().hide();
        _this.dom.overlay.hide();
        _this.dom.docHtml.removeClass('cxdialog_lock');
        $("#cxdialog").attr("style", "display: none;");
    };

    dialog.init();

    $.cxDialog = function () {
        dialog.format.apply(dialog, arguments);
    };

    $.cxDialog.close = function () {
        dialog.exit.apply(dialog);
    };
    $.cxDialog.initDialog = function () {
        dialog.init();
    };

    $.cxDialog.defaults = {
        title: '',
        info: '',
        ok: null,
        okText: '纭� 瀹�',
        no: null,
        noText: '鍙� 娑�',
        buttons: [],
        closeBtn: true,
        lockScroll: false,
        baseClass: '',
        background: '',
        width: 0,
        height: 0
    };
}));